
package com.zdb;


import com.zdb.utils.ChatRequestBuilder;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.json.JSONObject;

import java.io.IOException;

public class GetFileContent {

    private static final String API_KEY = "18c64c7b16d869f86953ade9571c3b87.FVhAx2TS7X7JB04n";
    private static final String FILE_ID = "1751523559_52eb7bc2fd92465c95f4de517b0cf092";
    private static final String ENDPOINT = "https://open.bigmodel.cn/api/paas/v4/chat/completions";

    public static void main(String[] args) {
        String url = "https://open.bigmodel.cn/api/paas/v4/files/" + FILE_ID + "/content";
        String message = null;
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);

            request.setHeader("Authorization", "Bearer " + API_KEY);
            request.setHeader("Accept", "application/json");

            ClassicHttpResponse response = httpClient.execute(request);

            int statusCode = response.getCode();
            System.out.println("Status Code: " + statusCode);

            if (response.getEntity() != null) {
                String jsonStr = EntityUtils.toString(response.getEntity());
                System.out.println("Raw JSON: " + jsonStr);

                // 解析 JSON
                JSONObject json = new JSONObject(jsonStr);
                String content = json.getString("content");
                message=content;
                System.out.println("File Content:");
                System.out.println(content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        demo(message);
    }
    public static void demo(String message) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost request = new HttpPost(ENDPOINT);

            request.setHeader("Authorization", "Bearer " + API_KEY);
            request.setHeader("Content-Type", "application/json");

            // 使用 ChatRequestBuilder 构造请求体
            String jsonBody = new ChatRequestBuilder("glm-4")
                    .addMessage("user", "请帮我总结这个故事：" + message)
                    .build();

            request.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));

            ClassicHttpResponse response = httpClient.execute(request);
            int statusCode = response.getCode();
            System.out.println("Status Code: " + statusCode);

            if (response.getEntity() != null) {
                String result = EntityUtils.toString(response.getEntity());
                System.out.println("Response Body: " + result);
            }

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }}